#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "common/parameters.h"
#include "common/types.h"
#include "common/xmlparser.h"


#include "mvc/view/principal_window.h"
#include "parameters_controller.h"
#include "principal_controller.h"
#include "configuration_controller.h"
#include <iostream>
#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QVector>
#include <QProcess>
#include <QProgressDialog>


class View;
class Model;

class Controller: public QObject
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = 0);
    ~Controller();

    //seters
    void set_model(Model *_model);
    void set_view(View *_view);

    //methods
    void connect_signals();
    QVector<Parameters> model_load_parameters(QVector<Parameters> parameters_uav);

signals:
    void s_update_wait_dialog(int _sim_step, QString output);

private:
    Model *_model;
    View *_view;
    Principal_controller *_principal_controller;
    Parameters_controller *_parameters_controller;
    Configuration_controller *_configuration_controller;
    configurations_parameters_uavs *_cpu;
    QProgressDialog *_wait_dialog;
    QTimer *_timer;
    QProcess *_uav_process;
    int _sim_step;
    int current_configuration;
    bool canceled_process;
    void initialize_timer();
    double calculate_viewing_time();
    void run_proces(int configuration, QString process_name);
    void start_wait_dialog();
    void simulate_configuration(int configuration, configurations_parameters_uavs *cpu);

private slots:
    void create_new_project(const QString &project_name, const QString &module);
    void open_project(const QString &project_name, int module);
    void save_project();
    void update_configuration(int configuration, void *values);
    void simulate(int configuration, configurations_parameters_uavs *_wait_dialog);
    void start_simulation(int configuration);
    void next_step(int configuration);
    void rewind_simulation(int configuration);
    void load_parameters_widget(ParametersWidget *parameters,  Airfoils_curve_map *airfoil_point_list, int module);
    void load_configuration_to_view(int configuration);
    void draw_configuration(int configuration);
    void process_finished(int exit_code, QProcess::ExitStatus exitStatus);
    void process_error(QProcess::ProcessError error_code);
    void readyReadStandardOutput();
    void readyReadStandardError();
    void update_wait_dialog(int _sim_step, QString output);
    void wait_dialog_canceled();
    void add_simulation_zones_to_model(int, QVector<time_zone*>);

public slots:
    void listen_view();
};

#endif // CONTROLLER_H
